<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <crudOperation :permission="permission">
        <el-button
          slot="left"
          class="filter-item"
          size="mini"
          type="primary"
          @click="crud.toDetail('financeSupplierPaymentDetail')"
        >
          新增
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          size="mini"
          type="primary"
          @click="crud.toPrint()"
        >
          打印
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          size="mini"
          type="primary"
          @click="crud.toPrint()"
        >
          打印预览
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          size="mini"
          :loading="crud.rejectLoading"
          @click="audit('CLOSE','作废')"
          :disabled="crud.selections.length < 1"
          v-permission="permission.invalid"
        >
          作废
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          size="mini"
          type="danger"
          :loading="crud.rejectLoading"
          icon="el-icon-remove-outline"
          @click="audit('PASS','审核')"
          :disabled="crud.selections.length < 1"
          v-permission="permission.pass"
        >
          审核
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          size="mini"
          :loading="crud.rejectLoading"
          type="success"
          icon="el-icon-circle-check"
          :disabled="crud.selections.length < 1"
          @click="audit('REJECT','反审')"
          v-permission="permission.reject"
        >
          反审
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          size="mini"
          type="warning"
          icon="el-icon-notebook-2"
          :disabled="crud.selections.length < 1"
          @click="tabInvoiceType('collectPrint',0)"
        >汇总打印</el-button>
      </crudOperation>
      <div v-if="!crud.props.searchToggle">
        <el-form size="small" label-width="80px" v-formkey="{query:crud.toQuery}">
          <el-row :gutter="10">
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="业务日期">
                <div class="flex flex-row">
                  <el-date-picker
                    v-model="query.startTime"
                    align="right"
                    unlink-panels
                    class="fl1"
                    format="yyyy - MM - dd "
                    value-format="yyyy-MM-dd"
                    default-value
                    range-separator=""
                    placeholder="开始日期"
                  />
                  <el-date-picker
                    v-model="query.endTime"
                    align="right"
                    unlink-panels
                    class="fl1"
                    format="yyyy - MM - dd "
                    value-format="yyyy-MM-dd"
                    default-value
                    range-separator=""
                    placeholder="结束日期"
                  />
                </div>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="单据编号">
                <el-input v-model="query.code" placeholder="输入单据编号搜索"/>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="供应商名称">
                <zSelect :value.sync="query.customerId" :showName="query.customerName" @getChange="getInfo"  url="api/dropDown/basCustomerList"  :params="{isContact:'1,2'}"
                         style="width:  100%"/>
              </el-form-item>
              <el-form-item label="供应商">
                <table-select
                  :tableColumns="
                  [
                  {prop:'code',label:'供应商编码',width: '100'},
                  {prop:'name',label:'供应商名称',width: '400'},
                  ]"
                  labelKey="name"
                  valueKey="id"
                  width="600"
                  :value.sync="query.customerIds"
                  placeholder="请选择供应商"
                  :params="{isContact: '1,2'}"
                  url="api/dropDown/basCustomerList"
                ></table-select>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="状态">
                <el-select  multiple
                            v-model="query.orderStatus"
                            class="edit-input"
                            placeholder="请选择单据状态"
                            clearable style="width: 100%;">
                  <el-option
                    v-for="(item,index) in dict.common_order_status"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10" style="text-align: center">
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="付款类型">
                <el-select v-model="query.type" class="filter-item" clearable placeholder="请选择付款类型" size="small"
                           style="width: 100%;">
                  <el-option
                    v-for="item in dict.t_finance_supplier_payment_type"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
<!--            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="银行账号">
                <el-input v-model="query.bankAccount" placeholder="输入银行账号搜索"/>
              </el-form-item>
            </el-col>-->
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="备注">
                <el-input v-model="query.remark" placeholder="输入备注搜索"/>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="人员">
                <zSelect :value.sync="userIds" multiple url="/api/dropDown/sysUserList" style="width:  100%;"/>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="创建人">
                <zSelect :value.sync="createUserIds" multiple url="/api/dropDown/sysUserList" style="width:  100%;"/>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-button class="filter-item"  size="mini" type="primary" @click="queryReport">搜索</el-button>
              <el-button class="filter-item" plain   size="mini" type="primary"
                         @click="resetQuery" >重置</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <el-dialog
        title="请选择打印模板"
        :visible.sync="crud.printConfig.pringDialog"
        width="30%"
      >
        <jPrintConfig groupId="financeSupplierPayment"
                      v-if="crud.printConfig.pringDialog"
                      @getPrintId="crud.toPrint"
                      :isPreview="crud.printConfig.isPreview"
        ></jPrintConfig>
      </el-dialog>
      <el-dialog
      title="请选择打印模板"
      :visible.sync="pringDialog"
      width="30%"
    >
      <jPrintConfig groupId="financeSupplierPayment"
                    v-if="pringDialog"
                    @getPrintId="print"
      ></jPrintConfig>
    </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" border v-loading="crud.loading" :data="crud.data" height=700 size="small" style="width: 100%;"
                @row-dblclick="crud.toDetailPlus('financeSupplierPaymentDetail',$event.id)"
                :summary-method="crud.sumRow"
                show-summary
                @row-click="crud.selectRow"
                :row-style="crud.rowClass"
                :row-class-name="crud.tableRowClassName"
                @selection-change="crud.selectionChangeHandler">
        <el-table-column :render-header="crud.renderHeader" type="selection" width="60"/>
        <el-table-column :render-header="crud.renderHeader" type="index" width="60" label="序号"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('code')" prop="code" label="单据编号"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('busDate')" prop="busDate" label="业务日期">
           <template slot-scope="scope">
            <span>{{ parseTime(scope.row.busDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('type')" prop="type" label="付款类型">
          <template slot-scope="scope">
            <span v-if="scope.row.type === 1">货款</span>
            <span v-if="scope.row.type === 2">预付款</span>
          </template>
        </el-table-column>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('bankName')" prop="bankName" label="银行名称"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('customerCode')" prop="customerCode" label="客户/供应商编码"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('customerName')" prop="customerName" label="供应商名称"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('taxAmount')" prop="taxAmount" label="金额"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('cancelAmount')" prop="cancelAmount" label="已核销金额"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('unCancelAmount')" prop="unCancelAmount" label="未核销金额"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('remark')" prop="remark" label="备注"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('orderStatus')" prop="orderStatus" label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.orderStatus === 'SAVE'">保存</span>
            <span v-if="scope.row.orderStatus === 'PASS'">审核</span>
            <span v-if="scope.row.orderStatus === 'CLOSE'">关闭</span>
          </template>
        </el-table-column>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('isPrint')" prop="isPrint" label="是否打印">
          <template slot-scope="scope">
            <span v-if="scope.row.isPrint === 0">否</span>
            <span v-if="scope.row.isPrint === 1">是</span>
          </template>
        </el-table-column>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('printCount')" prop="printCount" label="打印次数"/>
        <el-table-column :render-header="crud.renderHeader" v-permission="['admin','financeSupplierPayment:edit','financeSupplierPayment:del']" label="操作"
                         width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>
  </div>
</template>

<script>
import crudFinanceSupplierPayment from '@/api/erp/financeSupplierPayment'
import CRUD, {crud, form, header, presenter} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import jPrintConfig from '@/components/jPrintConfig/jPrintConfig'

// crud交由presenter持有
const defaultCrud = CRUD({
  title: '付款单',
  url: 'api/financeSupplierPayment',
  sort: 'id,desc',
  optShow: {
    add: false,
    edit: false,
    del: true,
    download: true,
    leadingIn: true,
    enable: false,
    disable: false,
    pass: false,
    reject: false,
    print: true,
    printDetail: true
  },
  query: {
    dataExtent: null,
    sourceCode: null,
    customerName: null,
    orderStatus: null,
    bankAccount: null
  },
  crudMethod: {...crudFinanceSupplierPayment}
})
const defaultForm = {
  id: null,
  code: null,
  name: null,
  type: null,
  busDate: null,
  sourceType: null,
  sourceId: null,
  sourceCode: null,
  customerId: null,
  customerCode: null,
  customerName: null,
  isContact: null,
  customerAreaId: null,
  customerAreaCode: null,
  customerAreaName: null,
  customerTypeId: null,
  customerTypeCode: null,
  customerTypeName: null,
  pCustomerId: null,
  pCustomerCode: null,
  pCustomerName: null,
  mCustomerId: null,
  mCustomerCode: null,
  mCustomerName: null,
  sCustomerId: null,
  sCustomerCode: null,
  sCustomerName: null,
  deptId: null,
  deptCode: null,
  deptName: null,
  userId: null,
  userCode: null,
  userName: null,
  taxAmount: null,
  tailAmount: null,
  cancelAmount: null,
  unCancelAmount: null,
  cancelStatus: null,
  cancelDate: null,
  cancelUserId: null,
  cancelUserCode: null,
  cancelUserName: null,
  isPrint: null,
  printCount: null,
  seq: null,
  orderStatus: null,
  remark: null,
  orderRemark: null,
  createUserId: null,
  createUserCode: null,
  createUserName: null,
  createTime: null,
  updateUserId: null,
  updateUserCode: null,
  updateUserName: null,
  updateTime: null,
  auditUserId: null,
  auditUserCode: null,
  auditUserName: null,
  auditTime: null,
  closeUserId: null,
  closeUserCode: null,
  closeUserName: null,
  closeTime: null,
}
export default {
  name: 'FinanceSupplierPayment',
  components: {pagination, crudOperation, rrOperation, udOperation, jPrintConfig},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  dicts: ['common_order_status', 't_finance_supplier_payment_type'],
  data() {
    return {
      invoiceType: "collectPrint",
      printIds:[],
      pringDialog: false,
      enable: false,
      userIds: [],
      createUserIds: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      permission: {
        add: ['admin', 'financeSupplierPayment:add'],
        edit: ['admin', 'financeSupplierPayment:edit'],
        del: ['admin', 'financeSupplierPayment:del'],
        import: ['admin', 'financeSupplierPayment:import'],
        export: ['admin', 'financeSupplierPayment:export'],
        copy: ['admin', 'financeSupplierPayment:copy'],
        upload: ['admin', 'financeSupplierPayment:upload'],
        download: ['admin', 'financeSupplierPayment:download'],
        pass: ['admin', 'financeSupplierPayment:pass'],
        reject: ['admin', 'financeSupplierPayment:reject'],
        closed: ['admin', 'financeSupplierPayment:closed'],
        invalid: ['admin', 'financeSupplierPayment:invalid'],
        stop: ['admin', 'financeSupplierPayment:stop'],
        recover: ['admin', 'financeSupplierPayment:recover'],
      },
      rules: {}
    }
  },
  updated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  watch: {
    userIds(n,o) {
      if (n.length) {
        this.query.userIds = this.userIds.join(',')
      } else {
        this.query.userIds = null
      }
    }
  },
  methods: {
    tabInvoiceType(val,isPreview){
      this.invoiceType = val
      this.isPreview = isPreview
      this.pringDialog = true;
    },
    print(val){
      if(val !== null){
        this.pringDialog=false
        localStorage.setItem(this.invoiceType,val)
        this.crud.doPrint(val,this.printIds,this.isPreview,this.invoiceType)
      }else {
        this.$message.error('请选择打印模板');
      }
    },
    queryReport(){
      this.query.createUserIds = this.createUserIds.length?this.createUserIds.join(','):''
      this.query.userIds = this.userIds.length?this.userIds.join(','):''
      this.crud.toQuery()
    },
    resetQuery(){
      this.userIds = []
      this.createUserIds = []
      this.crud.resetQuery()
    },
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
    },
    getValue(e) {
      this.query.startTime = this.query.dataExtent[0]
      this.query.endTime = this.query.dataExtent[1]
    },
    audit(type,msg){
      this.crud.rejectLoading = true
      const ids = this.crud.selections.map(m=>m.id)
      const data = {
        ids:ids,
        type:type
      }
      crudFinanceSupplierPayment.passOrReject(data).then(res=>{
        console.log(res)
        if(200 === res.status){
          this.crud.refresh()
          this.$message.success(msg+"成功")
        }
        this.crud.rejectLoading = false
      }).catch(e=>{
        this.crud.rejectLoading = false
      })
    }
  }
}
</script>

<style scoped>
.table-img {
  display: inline-block;
  text-align: center;
  background: #ccc;
  color: #fff;
  white-space: nowrap;
  position: relative;
  overflow: hidden;
  vertical-align: middle;
  width: 32px;
  height: 32px;
  line-height: 32px;
}
</style>
